package com.smedi.ismedi.inmail.core.application.query.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smedi.ismedi.inmail.api.dto.request.QueryListTaskRecordRequest;
import com.smedi.ismedi.inmail.api.dto.request.QueryPageTaskRecordRequest;
import com.smedi.ismedi.inmail.core.application.query.TaskQueryService;
import com.smedi.ismedi.inmail.core.application.query.vo.TaskRecordCountVo;
import com.smedi.ismedi.inmail.core.application.query.vo.TaskRecordVo;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.TaskRecord;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.TaskRecordEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.TaskRecordMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class TaskQueryServiceImpl implements TaskQueryService {
    private final DozerUtil dozerUtil;

    private final TaskRecordMapper taskRecordMapper;

    @Override
    public TaskRecordCountVo getTaskRecordCount(Long id) {
        TaskRecordCountVo taskRecordCountVo = new TaskRecordCountVo();

        QueryListTaskRecordRequest queryListTaskRecordRequest = new QueryListTaskRecordRequest();
        queryListTaskRecordRequest.setDealPersonId(id);
        queryListTaskRecordRequest.setTaskDealStatus("全部");
        List<TaskRecordEntity> taskRecordEntities = taskRecordMapper.doList(queryListTaskRecordRequest);

        taskRecordCountVo.setAllCount(taskRecordEntities.stream().count());
        taskRecordCountVo.setTodoCount(taskRecordEntities.stream().filter(x -> "待我处理".equals(x.getSourceFrom())).count());
        taskRecordCountVo.setDoneCount(taskRecordEntities.stream().filter(x -> "我处理的".equals(x.getSourceFrom())).count());
        taskRecordCountVo.setCreatedCount(taskRecordEntities.stream().filter(x -> "我发起的".equals(x.getSourceFrom())).count());
        taskRecordCountVo.setNoticeCount(taskRecordEntities.stream().filter(x -> "抄送我的".equals(x.getSourceFrom())).count());

        return taskRecordCountVo;
    }

    @Override
    public List<TaskRecordVo> queryListTaskRecord(QueryListTaskRecordRequest queryListTaskRecordRequest) {
        List<TaskRecordEntity> taskRecordEntities = taskRecordMapper.doList(queryListTaskRecordRequest);
        /*taskRecordEntities.stream().filter(x -> x.getExpireTime() != null && x.getExpireTime().toLocalDate().isBefore(LocalDate.now()) && ((!"已删除".equals(x.getTaskStatus()) && !"已完成".equals(x.getTaskStatus()) && !"已关闭".equals(x.getTaskStatus()))
        )).forEach(x -> x.setTaskStatus("已逾期"));*/
        return dozerUtil.convertor(taskRecordEntities, TaskRecordVo.class);
    }

    @Override
    public IPage<TaskRecordVo> queryPageTaskRecord(QueryPageTaskRecordRequest queryPageTaskRecordRequest) {
        IPage<TaskRecordVo> iPage = new Page<>(queryPageTaskRecordRequest.getPageNum(), queryPageTaskRecordRequest.getPageSize());
        IPage<TaskRecordEntity> taskRecordEntityIPage = taskRecordMapper.doListPage(iPage, queryPageTaskRecordRequest);
        /*List<TaskRecordEntity> records = taskRecordEntityIPage.getRecords();
        records.stream().filter(x -> x.getExpireTime() != null && x.getExpireTime().toLocalDate().isBefore(LocalDate.now()) && ((!"已删除".equals(x.getTaskStatus()) && !"已完成".equals(x.getTaskStatus()) && !"已关闭".equals(x.getTaskStatus()))
        )).forEach(x -> x.setTaskStatus("已逾期"));*/
        iPage.setRecords(dozerUtil.convertor(taskRecordEntityIPage.getRecords(), TaskRecordVo.class));
        return iPage;
    }

}
